package com.cantrill.ssofilter;

import java.io.IOException;

import javax.management.relation.RoleStatus;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Filter which authenticates user requests.
 * @author subin
 *
 */
public class SSOFilter implements Filter {

	private static String loginpage;
	private static String servicekey;
	private static String serviceurl;

	private static String loginapp;
	private static String registerapp;

	private static String dserviceurl;

	public static final String COOKIENAME="sess_id";
	@Override
	public void init(FilterConfig config) throws ServletException {

		setLoginpage(config.getInitParameter("loginpage"));
		setServicekey(config.getInitParameter("servicekey"));
		setServiceurl(config.getInitParameter("serviceurl"));
		setLoginapp(config.getInitParameter("loginapp"));
		setRegisterapp(config.getInitParameter("registerapp"));
	}


	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException 
	{
		HttpServletResponse hresponse=(HttpServletResponse)response;
		HttpServletRequest hrequest=(HttpServletRequest)request;
		String currentUrl=hrequest.getRequestURL().toString();
		String sessionid=null;
		String rolesstring="";
		try
		{
			if(request.getParameter("authticket")!=null)
			{
				sessionid=SSOUtil.validateAuthTicketAndGetSessionId(request, servicekey);
				if(sessionid==null)
				{
					hresponse.sendRedirect(SSOUtil.addParameter(loginpage, "serviceurl", currentUrl));
					
				}
				Cookie cookie=new Cookie(COOKIENAME, sessionid);
//				cookie.setDomain(".appspot.com");
//				cookie.setPath("/");
//				cookie.setValue(sessionid);
//				cookie.setMaxAge(3600*24*7);
				cookie.setPath("/");
				hresponse.addCookie(cookie);
				String redirecturl=currentUrl;
				if(redirecturl==null|| redirecturl.equals(""))
				{
					redirecturl=getServiceurl();
				}
				hresponse.sendRedirect(redirecturl);
				return;
			}
		
			sessionid=SSOUtil.getSessionIdFromCookie(hrequest, hresponse);
		}
		catch (Exception e) {
				hresponse.sendRedirect(SSOUtil.addParameter(loginpage, "serviceurl", currentUrl));
				return;
		}


		Boolean status=false;
		String resp=SSOUtil.validateSession(sessionid,servicekey,hrequest);
		if("error".equals(resp))
		{
			status=false;
		}
		else
		{
			status=true;
		}
		if(status)
		{
			rolesstring=SSOUtil.getRolesString(hrequest,hresponse,resp,servicekey);
			chain.doFilter(new SSORequest(hrequest,resp,rolesstring,sessionid), response);
		}
		else
		{
			hresponse.sendRedirect(SSOUtil.addParameter(loginpage, "serviceurl", currentUrl));
		}
	}

	










@Override
public void destroy() {
	// TODO Auto-generated method stub

}


public static void setLoginpage(String loginpage) {
	SSOFilter.loginpage = loginpage;
}


public static String getLoginpage() {
	return loginpage;
}


public static void setServicekey(String servicekey) {
	SSOFilter.servicekey = servicekey;
}


public static String getServicekey() {
	return servicekey;
}


public static void setServiceurl(String servicekey) {
	SSOFilter.serviceurl = servicekey;
}


public static String getServiceurl() {
	return serviceurl;
}


public static void setLoginapp(String loginapp) {
	SSOFilter.loginapp = loginapp;
}


public static String getLoginapp() {
	return loginapp;
}


public static void setRegisterapp(String serviceapp) {
	SSOFilter.registerapp = serviceapp;
}


public static String getRegisterapp() {
	return registerapp;
}


public static void setDserviceurl(String dserviceurl) {
	SSOFilter.dserviceurl = dserviceurl;
}


public static String getDserviceurl() {
	return dserviceurl;
}
}
